package com.asa.ui.H;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JFrame;
import javax.swing.JPanel;

import com.asa.ui.frame.AlgoVisHelper;

public class AlgoFrame extends JFrame{
	/**
	 * 
	 */
	private static final long serialVersionUID = -3035088527551930125L;
	private int canvasWidth;//画布的宽
	private int canvasHeight;//画布的长
	private GameData data;
   public int getCanvasWidth() {
		return canvasWidth;
	}
	public int getCanvasHeight() {
		return canvasHeight;
	}
	public void render(GameData data){
		repaint();
	}
   public AlgoFrame(GameData data,String title,int  canvasWidth, int canvasHeight){
	  super(title);
	  this.canvasWidth=canvasWidth;
	  this.canvasHeight=canvasHeight;
	  this.data = data;
	  setDefaultCloseOperation(EXIT_ON_CLOSE);
	  setResizable(false);//关闭可拖动
	  setVisible(true);
	  AlgoCanvas algoCanvas = new AlgoCanvas();
	  setContentPane(algoCanvas);
	  pack();
   }
   //设置面板JPanel
   class AlgoCanvas extends JPanel{
		/**
		 * 
		 */
	   private HashMap<Character,Color> colorMap;
	   private ArrayList<Color> colorList;

	   
		private static final long serialVersionUID = -6056196800598676936L;
	    private  AlgoCanvas(){
	    	//开启双缓存
	    	super(true);
	    	colorMap = new HashMap<>();
	    	colorList = new ArrayList<Color>();
	    	colorList.add(Color.RED);
	    	colorList.add(Color.PINK);
	    	colorList.add(Color.BLUE);
	    	colorList.add(Color.LIGHT_GRAY);
	    	colorList.add(Color.CYAN);
	    	colorList.add(Color.GREEN);
	    	colorList.add(Color.BLACK);
	    	colorList.add(Color.ORANGE);

	    	
	    	
	    }
		@Override
		//在面板上绘制内容
		protected void paintComponent(Graphics g) {
			super.paintComponent(g);
			Graphics2D g2d=(Graphics2D) g;
			AlgoVisHelper.setRenderingHints(g2d);//设置抗锯齿
			AlgoVisHelper.setStrokeWidth(g2d, 1);//设置边的宽度为5
			
			//具体绘制
			int w = canvasWidth/data.getM();
			int h = canvasHeight/data.getN();
			
			
			Board showBoard = data.getShowBoard();
			
			for (int i = 0; i < showBoard.getN(); i++) {
				for (int j = 0; j < showBoard.getM(); j++) {
					
					char c =showBoard.getData(i, j);
					if (c!=Board.EMPTY) {
						
						if (!colorMap.containsKey(c)) {
							int sz = colorMap.size();
							colorMap.put(c, colorList.get(sz));
						}
						
						Color color = colorMap.get(c);
						AlgoVisHelper.setColor(g2d, color);
						AlgoVisHelper.fillReactangle(g2d, j*h+2, i*w+2, w-4, h-4);
						
						
						AlgoVisHelper.setColor(g2d, Color.WHITE);
						String text = String.format("(%d,%d)", i,j);
						AlgoVisHelper.drawString(g2d, text, j*h+h/2, i*w+w/2);
						
					}
					
					
				}
			}
			
			
		}
		@Override
		public Dimension getPreferredSize() {
			return new Dimension(canvasWidth,canvasHeight);
		}
	   }
	  
}
